window.onload = function()
{
    $(document).scroll(function()
    {
        var $goTop = $('#go-top-img');

        if ($(document).scrollTop() > ($(window).height() / 7)){
            if ($goTop.css('display') == 'none') {
                $goTop.fadeIn(400);
            }
        } else {
            if ($goTop.css('display')  != 'none') {
                $goTop.hide(400);
            }
        }
    });

    $('#go-top-img').live('click', function()
    {
        $('html,body').animate({scrollTop: 0}, 600);
    });
};

window.redirect = function(url, time)
{
    /http:\/\/(.+?)\/.+/.exec(window.location.href);
    var goUrl = 'http://' + RegExp.$1;
    if ($('#root').length > 0) {
        goUrl += $('#root').val();
    }

    goUrl += url;
    console.log(goUrl);
    setTimeout('window.location.href="' + goUrl + '";', time);
};

window.checkDate = function(str, offset)
{
    console.log("start check date " + str);
    var datePattern = /^(\d{1,2})\/(\d{1,2})\/(\d{4})$/;
    var match = datePattern.exec(str);

    if (match == null) {
        return false;
    }

    var day = match[1];
    var month = match[2];
    var year = match[3];

    // Check month range
    if (month < 1 || month > 12) {
        return false;
    }

    if (day < 1 || day > 31) {
        return false;
    }

    if ((month == 4 || month == 6 || month == 9 || month == 11) && day == 31) {
        return false;
    }

    if (month == 2) {
        var isLeap = (year % 4 == 0 &&(year % 100 != 0 || year % 400 == 0));
        if (day > 29 || (day == 29 && !isLeap)) {
            return false;
        }
    }

    var today = new Date();

    console.log("Hehe");
    if (offset == undefined ) {
        offset = 0;
    }

    if (year >= today.getFullYear() - offset) {
        return false;
    }


    return true;
};

window.getYear = function(str)
{
    var match = /(\d{4})$/.exec(str);
    return match[1];
};